// autoBuild.js
var http = require('http')
var spawn = require('child_process').spawn
var createHandler = require('gitee-webhook-handler')
var handler = createHandler({ path: '/pushCode', secret: 'tiantian' }) // 在代码仓库的 Webhooks 选项处配置
http.createServer(function (req, res) {
  console.log('createServer')
  handler(req, res, function (err) {
    res.statusCode = 404;
    res.end('no such location')
  })
}).listen(1111)

handler.on('error', function (err) {
  console.error('Error:', err.message)
})

// 监听 push 事件 git
// handler.on('push', function (event) {
//   console.log('Received a push event for %s to %s',
//   event.payload.repository.name,
//   event.payload.ref)
//   rumCommand('sh', ['./autoBuild.sh'], function( txt ) { // 执行 autoBuild.sh 脚本文件
//     console.log(txt)
//   })
// })

function rumCommand( cmd, args, callback ) {
  console.log('进到这个节目啦')
  var child = spawn( cmd, args )
  var response = ''
  child.stdout.on('data', function( buffer ){ response += buffer.toString(); })
  child.stdout.on('end', function(){ callback( response ) })
}


handler.on('Push Hook', function (event) {
  console.log('Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref)

  rumCommand('sh', ['./autoBuild.sh'], function( txt ) { // 执行 autoBuild.sh 脚本文件
    console.log(txt)
  })
})
 
handler.on('Issue Hook', function (event) {
  console.log('Received an issue event for %s action=%s: #%d %s',
    event.payload.repository.name,
    event.payload.action,
    event.payload.issue.number,
    event.payload.issue.title)
})